package com.wxxymaker.service;

import com.blade.ioc.annotation.Bean;
import com.blade.ioc.annotation.Inject;
import com.blade.kit.EncryptKit;
import com.wxxymaker.model.dao.AdminDao;
import com.wxxymaker.model.entity.Admin;
import io.github.biezhi.anima.Anima;

/**
 * @Author: Soft
 * @Date: 2018/6/2 21:34
 * @Desc: 管理员业务层接口
 */
@Bean
public class AdminService {

    @Inject
    private AdminDao dao;
    // 账号密码登录
    public Admin check(String username, String MD5Pass) {
        Admin admin = dao.findByUsername(username);
        if (admin != null && admin.getId() > 0) {
            if (EncryptKit.md5(admin.getPass()).equals(MD5Pass)) {
                return admin;
            }
        }
        return null;
    }

    // 管理员信息修改
    public boolean change(Admin admin) {
        if (admin != null) {
            return admin.updateById(admin.getId()) > 0;
        }
        return false;
    }

    public void addView() {
        Anima.execute("update admin set views = views + 1");
    }

    public int getView() {
        Admin one = Anima.select(Admin::getViews).from(Admin.class).one();
        if (one != null) {
            return one.getViews();
        }
        return 0;
    }
}
